//
//  HospitalViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/9.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "HospitalViewController.h"
#import <DOPDropDownMenu.h>
#import "DataManager.h"
#import <Masonry.h>
#import "NearbyTableViewCell.h"
#import "Hospital.h"
#import "DetailsTableViewController.h"

#define kShared [DataManager defaultManager]

@interface HospitalViewController ()<DOPDropDownMenuDelegate,DOPDropDownMenuDataSource,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) DOPDropDownMenu *downMenu;
@property (nonatomic,strong) NSArray *cityArray;
@property (nonatomic,strong) NSString *state;
@property (nonatomic,strong) UITableView *tableV;
@end

@implementation HospitalViewController

- (NSArray *)cityArray
{
    if(!_cityArray)
    {
        _cityArray = [NSArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
        _cityArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _cityArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"所有医院";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    [self addViews];
    
}

- (void)addViews
{
    // 添加下拉菜单
    self.downMenu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
    self.downMenu.delegate = self;
    self.downMenu.dataSource = self;
    [self.view addSubview:self.downMenu];
    [self.downMenu selectDefalutIndexPath];
    
    //添加tableView
    self.tableV = [[UITableView alloc] init];
    [self.view addSubview:self.tableV];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downMenu).offset(44);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    //注册Cell
    [self.tableV registerNib:[UINib nibWithNibName:@"NearbyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NearbyCell"];
}



#pragma mark下拉菜单代理
/**
 *  返回 menu 有多少列 ，默认1列
 */
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 1;
}
/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if(column == 0)
    {
        return self.cityArray.count;
    }
    else
        return 0;
}
/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    return [[self.cityArray objectAtIndex:indexPath.row] objectForKey:@"State"];
}
/** 新增
 *  当有column列 row 行 返回有多少个item ，如果>0，说明有二级列表 ，=0 没有二级列表
 *  如果都没有可以不实现该协议
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    if (column == 0) {
        
        return [[[self.cityArray objectAtIndex:row] objectForKey:@"Cities"] count];
    }
    return 0;
}
/** 新增
 *  当有column列 row 行 item项 title
 *  如果都没有可以不实现该协议
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
      return [[[[self.cityArray objectAtIndex:indexPath.row] objectForKey:@"Cities"] objectAtIndex:indexPath.item] objectForKey:@"city"];
    }
    return nil;
}
/**
 *  点击代理，点击了第column 第row 或者item项，如果 item >=0
 */
- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    NSString *city = nil;
    if (indexPath.item >= 0) {
        city = [[[[self.cityArray objectAtIndex:indexPath.row] objectForKey:@"Cities"] objectAtIndex:indexPath.item] objectForKey:@"city"];
    }else {
        self.state = [[self.cityArray objectAtIndex:indexPath.row] objectForKey:@"State"];
    }
    if(city != nil)
    {
        [kShared getCityHospitalArrayWithState:self.state City:city AndBlock:^{
            [self.tableV reloadData];
        }];
    }
}

#pragma mark tableView的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return kShared.cityHospitalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyCell" forIndexPath:indexPath];
    Hospital *hospital = kShared.cityHospitalArray[indexPath.row];
    cell.hospitalLabel.text = hospital.hospitalName;
    cell.numberLabel.text = [NSString stringWithFormat:@"%@ 位医生",hospital.doctorCount];
    cell.levelLabel.text = hospital.hospitalLevel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

//点击cell跳转到医院详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsTableViewController *details = [[DetailsTableViewController alloc] init];
    details.hospital = kShared.cityHospitalArray[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
